<?php
/**
 * 获取用户是否已经是第一次看引导动画了
 */
require_once dirname(__DIR__)."/login_filter.php";
//require_once dirname(dirname(__DIR__))."/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\sdk\FoundaoVideoSDK;
use app\cls\tom\FFmpeg;
use app\cls\tom\Config;
use app\model\Video_templet;
use app\model\UserVideo;

//接收参数
$video_url=$_REQUEST['video_url'];
if(empty($video_url)){
    Response::output(-1,"缺少参数");
}
//实例化数据库
$db = SysResource::getDB(false);
$Video_templet = new Video_templet($db);
//用户uuid
$uuid=$Auth['uuid'];
//先去查询视频模板
$result=$Video_templet->get_video_templet();
if(empty($result)){
    Response::output(-1,"系统出了点小问题，难过");
}
//
$FoundaoVideoSDK=new FoundaoVideoSDK();
//设置视频参数
$option1=[];
$option2=[];
$option3=[];

$len=count($result);
if($len==1){
    $videoUrl=$result[0]['video_url'];
    $video_x=$result[0]['x'];
    $video_y=$result[0]['y'];
    $video_arrs['width']=$result[0]['w'];
    $video_arrs['height']=$result[0]['h'];
    $video_arrs['img_url']=$video_url;
    $times=$result[0]['video_time'];
    $video_uuid=$result[0]['video_uuid'];
    $video_desc=$result[0]['video_title'];
    $gunalian_img_url=$result[0]['video_guanlian_pic'];
}else{
    //随机取一个整数
    $arrs=range(0,$len-1);
    $num=array_rand($arrs);
    $arr1=$result[$num];
    //
    $videoUrl=$arr1['video_url'];
    $video_x=$arr1['x'];
    $video_y=$arr1['y'];
    $video_arrs['width']=$arr1['w'];
    $video_arrs['height']=$arr1['h'];
    $video_arrs['img_url']=$video_url;
    $times=$arr1['video_time'];
    $video_uuid=$arr1['video_uuid'];
    $video_desc=$arr1['video_title'];
    $gunalian_img_url=$arr1['video_guanlian_pic'];
}

//先判断视频是否被旋转了
$ffmpeg = new FFmpeg(false);
$xuanzhuan=$ffmpeg->getVideoDetail($video_url,2);
if(!empty($xuanzhuan)){
    foreach($xuanzhuan['streams'] as $k=>$v){
        if($v['codec_type']=="video"){
            if($v['side_data_list'][0]['rotation']==90 || $v['side_data_list'][0]['rotation']=="-90"){
//                $coded_width=$v['coded_height'];
//                $coded_height=$v['coded_width'];
                $coded_width=$v['coded_width'];
                $coded_height=$v['coded_height'];
            }
        }
    }
}

//设置大视频参数
$FoundaoVideo = $FoundaoVideoSDK->createInputVideo($videoUrl,$option1);
$Foundao['FoundaoVideo']=$FoundaoVideo;
//设置小视频参数
$video_time=round($times,3);
$FoundaoTiezhi1 = $FoundaoVideoSDK->createInputLayer("video",$video_x,$video_y,0,$video_time*1000,$video_arrs);
$Foundao['FoundaoTiezhi'][]=$FoundaoTiezhi1;

//设置输出路径
$path=upload_dir("video","mp4");
$option4=[];
$option4['width']=$coded_width;
$option4['height']=$coded_height;
$ret1 = $FoundaoVideoSDK->createOutput(trim($path['savename'],"/"), "local", "video",$option4);
$Foundao['createOutput']=$ret1;
if (empty($ret1)) {
    Response::output(-1,$FoundaoVideoSDK->getError());
}
//提交给彬总接口处理
$job_id = $FoundaoVideoSDK->submitJob();
if(empty($job_id)){
    Response::output(-1,"提交失败，请重新提交一次",$_SERVER['SERVER_ADDR']);
}
//添加数据到数据库
$data['material_id']=$job_id;
$data['video_uuid']=$video_uuid;
$data['uuid']=$Auth['uuid'];
$data['nick_name']=$Auth['nick_name'];
$data['nick_pic']=$Auth['nick_pic'];
if(!empty($video_desc)){
    $data['video_desc']=htmlspecialchars($video_desc);
}
$data['join_type']=$join_sub_id;
$data['join_type_sub']=$join_sub;
$data['video_url']=$path['savehttp'];
$data['type']=2;
$data['is_dub']=1;
$data['is_job_status']=1;
$data['del1']=1;
$data['examine']=1;
$data['add_time']=date("Y-m-d H:i:s",time());
$db = SysResource::getDB(true);
$UserVideo=new UserVideo($db);
$res=$UserVideo->add_material($data);
if($res){
    $dd['job_id']=$job_id;
    $dd['move_name']=$path['movename'];
    $dd['video_url']=$path['savehttp'];
    $dd['guanlian_img_url']=$gunalian_img_url;
    Response::output(0,"添加成功2",$dd,$_SERVER['SERVER_ADDR']);
}else{
    Response::output(-1,"添加失败");
}



//获取上传目录
function upload_dir($type="tmp",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    $uploadpath['savename']=$dir."/".$file_name.".{$suffix}";
    $uploadpath['movename']=$uploadpath1.$dir;
    if($type=="video"){
        mkdir($uploadpath1.$dir."/".$file_name,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}
